package com.qfedu.dao.impl;

import com.qfedu.dao.NoticeDao;
import com.qfedu.model.Notice;
import com.qfedu.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author lenovo
 * @version 1.0
 * @date 2023/4/18 9:26
 * @poject bloo
 */
public class NoticeDaoImpl implements NoticeDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());
    @Override
    public int addNotice(Notice notice) {
        String sql="insert into notice values (null,?,?,?)";
        int update = jdbcTemplate.update(sql, notice.getTitle(), notice.getContent(), notice.getCretedUser());
        return update;
    }

    @Override
    public int updateNotice(Notice notice) {
        String sql="update notice set title=?,content=?,cretedUser=? where id=?";
        int update = jdbcTemplate.update(sql, notice.getTitle(), notice.getContent(), notice.getCretedUser(), notice.getId());
        return update;
    }

    @Override
    public int deleteNotice(int id) {
        String sql ="delete from notice where id=?";
        int update = jdbcTemplate.update(sql, id);
        return update;
    }

    @Override
    public List<Notice> findAllNotice() {
        String sql = "select * from notice";
        List<Notice> notices= jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Notice.class));
        return notices;
    }

    @Override
    public Notice findNoticeById(int id) {
        String sql = "select * from notice where id=?";
        List<Notice> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Notice.class), id);
        if (query.size() == 1 && query != null){
            return query.get(0);
        }
        return null;
    }
}